define(["spin", "jquery"], function(Spinner) {

    var Spin = function() {
        this.spin = new Spinner({
            lines: 15, // The number of lines to draw
            length: 21, // The length of each line
            width: 7, // The line thickness
            radius: 20, // The radius of the inner circle
            corners: 1, // Corner roundness (0..1)
            rotate: 0, // The rotation offset
            direction: 1, // 1: clockwise, -1: counterclockwise
            speed: 1.0, // Rounds per second
            trail: 60 // Afterglow percentage
        });
        $(document.body).append('<div id="spin-mask" style="display:none;position:fixed;top:50%;left:50%;z-index:200000;"></div>');
    };

    Spin.prototype.run = function(promise) {
        // start
        this.spin.spin($('#spin-mask').show()[0]);
        // stop
        var self = this;
        if (typeof promise === 'object' && promise.hasOwnProperty('finally')) {
            // this is a promise
            promise['finally'](function() {
                self.spin.stop();
            });
        } else {
            setTimeout(function() {
                self.spin.stop();
            }, 1000);
        }
        return promise;
    };

    Spin.prototype.stop = function() {
        $('#spin-mask').hide();
        this.spin.stop();
    };

    return new Spin();

});